package com.fusetech.web.flow.controller.audit;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fusetech.flow.audit.domain.AuditNode;
import com.fusetech.flow.audit.service.AuditNodeService;
import com.fusetech.flow.audit.service.dto.AuditNodeDTO;
import com.fusetech.flow.audit.service.mapstruct.AuditNodeWrapper;
import com.fusetech.os.core.query.QueryWrapperGenerator;
import com.fusetech.os.core.response.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

/**
 * 审核节点配置表Controller
 * @author Liuwq
 * @version 2023-05-08
 */

//@Api(tags ="审核节点配置表")
@RestController
@RequestMapping(value = "/erp/audit/auditNode")
public class AuditNodeController {

	@Autowired
	private AuditNodeService auditNodeService;

	@Autowired
	private AuditNodeWrapper auditNodeWrapperImpl;

	/**
	 * 审核节点配置表列表数据
	 */
	//@ApiLog("查询审核节点配置表列表数据")
	//@ApiOperation(value = "查询审核节点配置表列表数据")
	//@PreAuthorize({"erp:auditNode:list"})
	@GetMapping("list")
	public ResponseEntity<?> list(AuditNodeDTO auditNodeDTO, Page<AuditNodeDTO> page) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (auditNodeDTO, AuditNodeDTO.class);
		IPage<AuditNodeDTO> result = auditNodeService.findPage (page, queryWrapper);
		return ResponseMessage.OK(result);
	}

	//@ApiOperation(value = "查询审核节点配置表列表数据")
	//@PreAuthorize({"erp:auditNode:listNODE"})
	@GetMapping("listNODE")
	public ResponseEntity<?> listNODE(AuditNodeDTO auditNodeDTO, Page<AuditNodeDTO> page) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (auditNodeDTO, AuditNodeDTO.class);
		IPage<HashMap> result = auditNodeService.findPageNODE (page, queryWrapper);
		return ResponseMessage.OK(result);
	}

	/**
	 * 根据Id获取审核节点配置表数据
	 */
	//@ApiLog("根据Id获取审核节点配置表数据")
	//@ApiOperation(value = "根据Id获取审核节点配置表数据")
	//@PreAuthorize({"erp:auditNode:view","erp:auditNode:add","erp:auditNode:edit"})
	@GetMapping("queryById")
	public ResponseEntity<?> queryById(String id) {
		return ResponseMessage.OK( auditNodeService.findById ( id ) );
	}

	/**
	 * 保存审核节点配置表
	 */
	//@ApiLog("保存审核节点配置表")
	//@ApiOperation(value = "保存审核节点配置表")
	//@PreAuthorize({"erp:auditNode:add","erp:auditNode:edit"})
	@PostMapping("save")
	public ResponseEntity<?> save(@Valid @RequestBody AuditNodeDTO auditNodeDTO) {
	 AuditNode auditNode=auditNodeWrapperImpl.toEntity (auditNodeDTO);

		String auditProcessId = auditNodeDTO.getAuditProcessId();
		String maxNodeID = auditNodeService.getMaxNodeID(auditProcessId);
		if(StringUtils.isEmpty(auditNode.getId())){
			if(!StringUtils.checkValNull(maxNodeID)){
				int nodeID = Integer.parseInt(maxNodeID);
				nodeID = nodeID+1;
				auditNode.setNodeId(String.valueOf(nodeID));
			}else{
				auditNode.setNodeId("1");
			}
		}

		auditNode.setAuditProcessId(auditProcessId);
		if(auditNodeService.saveOrUpdate(auditNode)){
			String aid = auditNode.getId();
			auditNodeService.syncRoleAndAuditor(aid,auditNodeDTO);//同步保存最新关联角色及人员关系 del 关联角色及人员关系之后保存最新关系
		    return ResponseMessage.OK(auditNodeWrapperImpl.toDTO(auditNode));
		}{
		    return ResponseMessage.ERROR( "保存审核节点配置表失败" );   
		}
	}


	/**
	 * 删除审核节点配置表
	 */
	//@ApiLog("删除审核节点配置表")
	//@ApiOperation(value = "删除审核节点配置表")
	//@PreAuthorize({"erp:auditNode:del"})
	@DeleteMapping("delete")
	public ResponseEntity<?> delete(String ids) {//目前支持单个删除
			String id = ids;
			AuditNodeDTO an = auditNodeService.findById(id);
			String maxNodeId =auditNodeService.getMaxNodeID(an.getAuditProcess().getId());
			if(maxNodeId.equals(an.getNodeId())){//说明是最大步骤数
				auditNodeService.removeById(id);
				auditNodeService.delRoleAndAuditor(id);//del 关联
				return ResponseMessage.OK( "删除审核节点配置表成功" );
			}else {
				return ResponseMessage.ERROR( "请选择最大步骤数节点配置先删除" );
			}

	}
	/*public ResponseEntity<?> delete(String ids) {
		String idArray[] = ids.split(",");
		for (String id : idArray) {
			AuditNodeDTO an = auditNodeService.findById(id);
			String maxNodeId =auditNodeService.getMaxNodeID(an.getAuditProcess().getId());
			if(maxNodeId.equals(an.getNodeId())){

			}
		}
        auditNodeService.removeByIds ( Lists.newArrayList ( idArray ) );
		auditNodeService.delRoleAndAuditor(idArray);//del 关联
		return ResponseMessage.OK( "删除审核节点配置表成功" );
	}*/
	
	/**
     * 导出审核节点配置表数据
     *
     * @param auditNodeDTO
     * @param page
     * @param response
     * @throws Exception
     */
    //@ApiLog("导出审核节点配置表数据")
    //@PreAuthorize({"erp:auditNode:export"})
    /*@GetMapping("export")
    public void exportFile(AuditNodeDTO auditNodeDTO, Page <AuditNodeDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (auditNodeDTO, AuditNodeDTO.class);
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) { // 导出当前页数据
            
        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) { // 导出选中数据
            queryWrapper.in ( "a.id", options.getSelectIds () );
        } else { // 导出全部数据
            page.setSize ( -1 );
            page.setCurrent ( 0 );
        }
        List<AuditNodeDTO> result = auditNodeService.findPage ( page, queryWrapper ).getRecords ( );
        EasyExcelUtils.newInstance ( auditNodeService, auditNodeWrapperImpl ).exportExcel ( result,  options.getSheetName ( ), AuditNodeDTO.class, fileName,options.getExportFields (), response );
    }*/

    /**
     * 导入审核节点配置表数据
     *
     * @return
     */
    //@PreAuthorize({"erp:auditNode:import"})
    /*@PostMapping("import")
    public ResponseEntity importFile(MultipartFile file) throws IOException {
        String result = EasyExcelUtils.newInstance ( auditNodeService, auditNodeWrapperImpl ).importExcel ( file, AuditNodeDTO.class );
        return ResponseMessage.OK(result);
    }*/

    /**
     * 下载导入审核节点配置表数据模板
     *
     * @param response
     * @return
     */
    //@PreAuthorize({"erp:auditNode:import"})
    /*@GetMapping("import/template")
    public void importFileTemplate(HttpServletResponse response) throws IOException {
        String fileName = "审核节点配置表数据导入模板.xlsx";
        List<AuditNodeDTO> list = Lists.newArrayList();
        EasyExcelUtils.newInstance ( auditNodeService, auditNodeWrapperImpl ).exportExcel ( list,  "审核节点配置表数据", AuditNodeDTO.class, fileName, null, response );
    }*/

}
